//
//  MyStockRadarController.m
//  Stock
//
//  Created by blacksun on 11-7-6.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "MyStockRadarController.h"


@implementation MyStockRadarController
@synthesize parentController;
@synthesize myStock;
@synthesize table1;
@synthesize table2;
@synthesize stockRadars;


- (void)dealloc
{
    [table1 release];
    [table2 release];
    self.stockRadars = nil;
    
    [super dealloc];
}
-(IBAction)editStockRadar:(id)sender{
    if (![StockContext shareInstance].isFavorite) {
        [[StockAppHelper shareInstance] showAlertViewWithMessage:@"添加到自选股后才能设置预警雷达"];
        return;
    }
    UIButton *button = (UIButton*)sender;
    StockRadar *radar = [stockRadars objectAtIndex:button.tag];
    MyStockRadarEditControll *editController = [[MyStockRadarEditControll alloc] initWithNibName:@"MyStockRadarEditControll" bundle:nil];
    editController.radar = radar;
    editController.title = radar.name;
    StockAppDelegate *delegate = [UIApplication sharedApplication].delegate;
    [delegate.navigationController pushViewController:editController animated:YES];
    [editController release];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self performSelector:@selector(updateParent) withObject:nil afterDelay:0.1];
    [self viewWillAppear:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    //NSLog(@"radar will appear");
    [super viewWillAppear:animated];
    StockRadar *up = [StockRadar findByStockId:myStock.uuid andType:kStockMonitorTypeUp];
    StockRadar *down = [StockRadar findByStockId:myStock.uuid andType:kStockMonitorTypeDown];
    self.stockRadars = [NSArray arrayWithObjects:up,down, nil];
    [self.table1 reloadData];
    [self.table2 reloadData];
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
-(void)updateParent{
    [parentController childViewDidUpdated:self.view.frame.size.height];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    [table1 release]; 
    [table2 release];
    self.stockRadars = nil;
}

#pragma mark - tableView 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"stockRadarCell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont systemFontOfSize:17.0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(130.0, 12.0, 180.0, 20.0)];
        rightLabel.backgroundColor = [UIColor clearColor];
        rightLabel.tag = kRadarCellRightLabelTag;
        rightLabel.textColor = [UIColor whiteColor];
        rightLabel.textAlignment = UITextAlignmentRight;
        
        [cell.contentView addSubview:rightLabel];
        [rightLabel release];
    }
    int index = 0;
    if (aTableView==table2) {
        index = 1;
    }
    StockRadar *radar = [stockRadars objectAtIndex:index];
    radar.currentValue = [myStock.closingPrice stringValue];
    NSInteger row = indexPath.row;
    NSString *imageName = @"cellbg1.png";
    if (row%2==1) {
        imageName = @"cellbg2.png";
    }
    cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    
    UILabel *rightLabel = (UILabel *)[cell viewWithTag:kRadarCellRightLabelTag];
    rightLabel.hidden = YES;
    
    
    if (row==0) {
        cell.textLabel.text = @"当前价";
        rightLabel.hidden = NO;
        rightLabel.text = radar.currentValue;
    }else if (row==1) {
        cell.textLabel.text = @"预警阀值";
        rightLabel.hidden = NO;
        rightLabel.text = radar.targetValue;
    }/*else if (row==2) {
        cell.textLabel.text = @"预警差幅";
        rightLabel.hidden = NO;
        rightLabel.text = radar.targetPercent;
    }*/else if (row==2) {
        cell.textLabel.text = @"状态";
        rightLabel.hidden = NO;
        rightLabel.text = [radar statusName];
    } else if (row==3) {
        cell.textLabel.text = @"上次预警时间";
        rightLabel.hidden = NO;
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        rightLabel.text = [formatter stringFromDate:radar.lastDate];
        [formatter release];
    }
    return cell;
}

@end 